﻿using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SuperKit
{
    class CombinePDF : CommandExecutor {

        public CombinePDF(ArgReader arg, IExecutorOwner form, string fpath) : base(arg, form, fpath) {

        }

        public List<FileInfo> TargetList { get; private set; }
        public override void Execute() {
            base.Execute();
            TargetList = new List<FileInfo>();
            GetAllFiles(new DirectoryInfo(FilePath), TargetList, Kit.PDF_EXTS);
            Combine();
        }

        private void Combine() {
            var files = new string[TargetList.Count];
            for (int i = 0; i < files.Length; i++) {
                files[i] = TargetList[i].FullName;
            }
            var new1 = PdfDocumentBase.Merge(null, files);
            var savePath = CalculateOutput();
            new1.Save(savePath);
            Flush("Combine {0} PDFs to {1}", files.Length, savePath);

            End();
        }

        private string CalculateOutput() {
            var output = TargetDir;
            if (Kit.IsEmpty(output)) {
                return string.Format("{0}.pdf", FilePath);
            } else {
                var pure = Path.GetFileNameWithoutExtension(FilePath);
                return string.Format("{0}\\{1}.pdf", output, pure);
            }
        }

    }
}
